<?php
namespace Admin\Controller;

use Think\Controller;
/**
 * 影院管理
 * @author  zhouzhuan
 * @since   1.0
 */
class UserController extends Controller
{
    public function index()
    {
        $models = M('user');
        if(!empty($_GET['name'])){
            $this->assign('name',$_GET['name']);
        }
        if(!empty($_GET['address'])){
            $this->assign('address',$_GET['address']);
        }

             //模糊查询
            $data['address'] = array('like',"%{$_GET['address']}%");
            $data['name'] = array('like',"%{$_GET['name']}%");
        //分页
        $total=$models->where($data)->count();
        $page = new \Think\Page($total,10);
        $page->setConfig('header','共页数');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page_str = $page->show();

        $users = $models->where($data)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('page',$page);
        $this->assign('users',$users);
        $this->assign('active','user');
        $this->display();
    }

    public function create()
    {
        if (!empty($_POST)) {

            if(!empty($_POST['username'])){
                $_SESSION['username']=$_POST['username'];
            }
            if(!empty($_POST['password'])){
                $_SESSION['password']=$_POST['password'];
            }
            if(!empty($_POST['name'])){
                $_SESSION['name']=$_POST['name'];
            }
            if(!empty($_POST['address'])){
                $_SESSION['address']=$_POST['address'];
            }

            if (empty($_POST['username'])) {
                session('message', '用户名不能为空');
                $this->redirect('create');
                return false;
            }
            if (empty($_POST['password'])) {
                session('message', '密码不能为空');
                $this->redirect('create');
                return false;
            }
            if (empty($_POST['name'])) {
                session('message', '影院名称不能为空');
                $this->redirect('create');
                return false;
            }
            if (empty($_POST['address'])) {
                session('message', '地址不能为空');
                $this->redirect('create');
                return false;
            } else {
                $data['name'] = $_POST['name'];
                $data['address'] = $_POST['address'];
                $data['username'] = $_POST['username'];
                $data['password_hash'] = $_POST['password'];
                $data['created_at'] = date('Y-m-d H:i:s', time());
                $list = M('user')->add($data);
                if($list){
                    session('message','添加成功');
                    unset($_SESSION['username']);
                    unset($_SESSION['password']);
                    unset($_SESSION['name']);
                    unset($_SESSION['address']);
                }else{
                    session('message','添加失败');
                }

                $this->redirect('index');
            }
        }
        $this->assign('username',$_SESSION['username']);
        $this->assign('password',$_SESSION['password']);
        $this->assign('name',$_SESSION['name']);
        $this->assign('address',$_SESSION['address']);
        $this->display();
     }

    public function update(){
        $list= M('user')->where(['id'=>$_GET['id']])->find();
        if (!empty($_POST)) {
            if (empty($_POST['username'])) {
                session('message', '用户名不能为空');
                $this->redirect('update');
                return false;
            }
            if (empty($_POST['password'])) {
                session('message', '密码不能为空');
                $this->redirect('update');
                return false;
            }
            if (empty($_POST['name'])) {
                session('message', '影院名称不能为空');
                $this->redirect('update');
                return false;
            }
            if (empty($_POST['address'])) {
                session('message', '地址不能为空');
                $this->redirect('update');
                return false;
             } else {
                $data['name'] = $_POST['name'];
                $data['address'] = $_POST['address'];
                $data['username'] = $_POST['username'];
                $data['password_hash'] = $_POST['password'];
                $data['updated_at'] = date('Y-m-d H:i:s', time());
                $models = M('user')->where(['id' => $_GET['id']])->save($data);
                if(!$models){
                    session('message','亲~~你没有做任何修改哦！！');
                }else{
                    session('message','修改成功');
                }
                $this->redirect('index');
                }
          }
            $this->assign('list',$list);
            $this->display('update');
    }




}